package fr.upstlse.glre.client.administration.profil;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.ui.Composite;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.administration.component.Center;
import fr.upstlse.glre.client.administration.profil.action.AdminProfilEditionSubmit;
import fr.upstlse.glre.client.administration.utilisateurs.action.RoleSelectionAction;
import fr.upstlse.glre.client.espacededie.component.IUserModification;
import fr.upstlse.glre.client.vo.user.UserVOGWT;
import fr.upstlse.glre.client.widgets.fields.IUserFields;
import fr.upstlse.glre.client.widgets.fields.UserFields;

/**
 * Construit le panneau de profil d'un étudiant. Ce panneau affiche
 * textuellement le profil d'un étudiant. Il offre la possibilité de le modifier
 * par l'intermédiaire d'un formulaire
 * 
 * @author michael
 * 
 */
public class AdminProfil extends Composite implements IUserModification {

	/**
	 * Panneau offrant un certain nombre de widgets. Il contient le panneau de
	 * contenu.
	 */
	ContentPanel profilPanel;

	/**
	 * Panneau HTML contenant le profil au format texte.
	 */
	Html profilReadingPanel;

	/**
	 * Panneau FORM contenant le profil au format formulaire pour l'édition.
	 */
	FormPanel profilEditingPanel;

	UserVOGWT user;

	/**
	 * Variable d'état pour le panneau de contenu. Vrai si le panneau courant
	 * est l'édition du profil, faux sinon
	 */
	boolean editionMode = false;

	/**
	 * Construit le panneau de profil. L'initialise avec le contenu en mode
	 * lecture (affichage du profil) Initialise le service.
	 * 
	 * @param pEtudiant
	 *            Etudiant dont on souhaite obtenir le profil
	 */
	public AdminProfil(UserVOGWT pUser) {
		user = pUser;

		createProfilPanel();
		readingMode();
		editionMode();

		profilPanel.add(profilReadingPanel);
		editionMode = false;
		initWidget(profilPanel);
	}

	private void createProfilPanel() {
		profilPanel = new ContentPanel();
		profilPanel.setAutoWidth(true);
		profilPanel.setWidth(500);
		profilPanel.setId(RfgConstante.PROFIL_PANEL_ID);
		profilPanel.setBorders(true);

		// Draggable panel
		Draggable lDraggable = new Draggable(profilPanel, profilPanel
				.getHeader());
		lDraggable.setContainer(Center.getInstance());

		// Tool Button
		ToolButton lSwithToEditionMode = new ToolButton(".my-tool-gear");
		lSwithToEditionMode.setStyleAttribute("background",
				" transparent url(images/edit.png) no-repeat");
		lSwithToEditionMode
				.setToolTip(RfgConstante.PROFIL_PANEL_BUTTON_SWITCH_TOOLTIP);

		// SelectionListener<ComponentEvent> lSwitchModeListener = new
		// SelectionListener<ComponentEvent>();
		lSwithToEditionMode
				.addSelectionListener(new SelectionListener<ComponentEvent>() {
					public void componentSelected(ComponentEvent ce) {
						if (!editionMode) {

							profilPanel.add(profilEditingPanel);
							profilPanel.layout();
							profilPanel.remove(profilReadingPanel);
							profilPanel.layout();
							editionMode = true;
						} else {

							profilPanel.add(profilReadingPanel);
							profilPanel.layout();
							profilPanel.remove(profilEditingPanel);
							profilPanel.layout();
							editionMode = false;
						}
					}

				});
		profilPanel.setHeading(RfgConstante.PROFIL_PANEL_HEADER_TEXT);
		profilPanel.getHeader().addTool(lSwithToEditionMode);
	}

	private void editionMode() {
		// formulaire sans action
		profilEditingPanel = new FormPanel();
		profilEditingPanel.setHeaderVisible(false);
		profilEditingPanel.setAutoWidth(true);

		UserFields lUserFields = new UserFields(user, profilEditingPanel);

		lUserFields.getRole().addListener(Events.Select,
				new RoleSelectionAction(this));

		Button lSubmit = new Button(
				RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
		lSubmit.addSelectionListener(new AdminProfilEditionSubmit(lUserFields,
				this));
		profilEditingPanel.addButton(lSubmit);

	}

	public void readingMode() {
		if (user == null) {
			MessageBox.alert("Erreur accès admin",
					"L'admin n'est pas bien authentifié", null);
		} else {
			profilReadingPanel = user.getHtml();
		}
	}

	public ContentPanel getProfilPanel() {
		return profilPanel;
	}

	public void setProfilPanel(ContentPanel profilPanel) {
		this.profilPanel = profilPanel;
	}

	public Html getProfilReadingPanel() {
		return profilReadingPanel;
	}

	public void setProfilReadingPanel(Html profilReadingPanel) {
		this.profilReadingPanel = profilReadingPanel;
	}

	public FormPanel getProfilEditingPanel() {
		return profilEditingPanel;
	}

	public void setProfilEditingPanel(FormPanel profilEditingPanel) {
		this.profilEditingPanel = profilEditingPanel;
	}

	public UserVOGWT getUser() {
		return user;
	}

	public void setUser(UserVOGWT user) {
		this.user = user;
	}

	public boolean isEditionMode() {
		return editionMode;
	}

	public void setEditionMode(boolean editionMode) {
		this.editionMode = editionMode;
	}

	public IUserFields getAdditionalFields() {
		// TODO Auto-generated method stub
		return null;
	}

	public FormPanel getFormPanel() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setAdditionalFields(IUserFields additionalFields) {
		// TODO Auto-generated method stub

	}

	public void setFormPanel(FormPanel formPanel) {
		// TODO Auto-generated method stub

	}
}
